package com.example.articleservice.controller;


import com.example.articleservice.entity.Category;
import com.example.articleservice.service.ICategoryService;
import com.example.common.Ajaxresult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-02-18
 */
@RestController
@RequestMapping("/category")
public class CategoryController {
        @Autowired
        private ICategoryService iCategoryService;

        @RequestMapping("/add")
        public Ajaxresult add(Category category) {
                if (category == null || !StringUtils.hasLength(category.getName())) {
                        return Ajaxresult.fail(-1, "参数非法");
                }
                boolean result = iCategoryService.save(category);
                if (!result) {
                        return Ajaxresult.fail(-1, "未知错误");
                }
                return Ajaxresult.success(true);
        }

        @RequestMapping("/list")
        public Ajaxresult getlist() {
                List<Category> list = iCategoryService.list();
                Collections.reverse(list);
                return Ajaxresult.success(list);
        }

        @RequestMapping("/del")
        public Ajaxresult del(Integer cid) {
                if (cid == null) {
                        return Ajaxresult.fail(-1, "参数非法");
                }
                boolean result = iCategoryService.removeById(cid);
                if (!result) {
                        return Ajaxresult.fail(-1, "未知错误");
                }
                return Ajaxresult.success(true);

        }

}
